/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import com.arg.deploy.bean.User;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.util.Locales;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericAutowireComposer;

/**
 *
 * @author sorasaks
 */
public abstract class MainPageController extends GenericAutowireComposer<Component> {

    private static final Logger mLog = Logger.getLogger(MainPageController.class);
    public static final String SIGNIN_URL = "/signin";
    public static final String USER_URL = "/user";
    public static final String ADMIN_URL = "/admin";
    public static final String USER_PERMISSION = "USER_PERMISSION";
    public static final String DATE_FORMAT = "dd-MM-yyyy";
    private String pageName;
    private Boolean isResource;
    private Boolean isSignin;
    public Locale locale;
    protected User userByLogin;
    
    public MainPageController() {
        this.isSignin = Boolean.FALSE;
        initialParameter();
    }

    public MainPageController(String pageName, Boolean isResource) {
        this.pageName = pageName;
        this.isResource = isResource;
        this.isSignin = Boolean.FALSE;
        initialParameter();
        mLog.info("::::::::::::::::::::::: pageName = " + pageName);
        mLog.info("::::::::::::::::::::::: isResource = " + isResource);
        mLog.info("::::::::::::::::::::::: locale = " + locale);
    }

    public MainPageController(String pageName, Boolean isResource, Boolean isSignin) {
        this.pageName = pageName;
        this.isResource = isResource;
        this.isSignin = isSignin;
        initialParameter();
        mLog.info("::::::::::::::::::::::: pageName = " + pageName);
        mLog.info("::::::::::::::::::::::: isResource = " + isResource);
        mLog.info("::::::::::::::::::::::: locale = " + locale);
    }

    private void initialParameter() {
        setParameter();
        reloadParameter();
    }

    @Override
    public void doBeforeComposeChildren(Component comp) throws Exception {
        super.doBeforeComposeChildren(comp); //To change body of generated methods, choose Tools | Templates.
        checkPermission();
        if (!StringUtils.isEmpty(pageName)) {
            if (isResource) {
                setTitle(ResourceUtil.getLabel(pageName, locale));
            } else {
                setTitle(pageName);
            }
        }
    }

    private void checkPermission() {
        userByLogin = (User) Executions.getCurrent().getSession().getAttribute(USER_PERMISSION);
        mLog.info(":::::::::::::::::::::::::::::: user = " + userByLogin);
        if (userByLogin == null && !isSignin) {
            Executions.getCurrent().sendRedirect(SIGNIN_URL);
        } else if (userByLogin != null && isSignin) {
            if (userByLogin.getUsername().equals("admin")) {
                Executions.getCurrent().getSession().setAttribute(USER_PERMISSION, userByLogin);
                Executions.getCurrent().sendRedirect(ADMIN_URL);
            } else {
                Executions.getCurrent().getSession().setAttribute(USER_PERMISSION, userByLogin);
                Executions.getCurrent().sendRedirect(USER_URL);
            }
        }
    }

    protected void setTitle(String _pageName) {
        getPage().setTitle(_pageName);
    }

    private void setParameter() {
        locale = (Locale) Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
    }

    private void reloadParameter() {
        if (locale == null) {
            setLocale();
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, locale);
        }
    }

    private void setLocale() {
        Object obj = Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        Locale localeObj = Locales.getCurrent();
        if (obj == null) {
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, Locales.getCurrent());
        } else {
            localeObj = (Locale) obj;
        }
        this.locale = (localeObj == null ? Locale.US : localeObj);
    }
}
